package com.example.star.service;

import com.example.star.entity.User;
import com.example.star.entity.UserRole;
import com.example.star.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> getNormalUsers() {
        return userMapper.getNormalUsers();
    }

    public List<User> getAdminUsers() {
        return userMapper.getAdminUsers();
    }
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    // 注册用户
    public void register(User user) {
        userMapper.insertUser(user);
    }

    // 登录验证
    public Optional<User> login(String username, String password) {
        return userMapper.findByUsername(username)
                .filter(user -> user.getPassword().equals(password));
    }

    // 检查用户名是否已存在
    public boolean isUsernameExists(String username) {
        Optional<User> userOptional = userMapper.findByUsername(username);
        return userOptional.isPresent();
    }

    // 检查昵称是否已存在
    public boolean isNicknameExists(String nickname) {
        return userMapper.findByNickname(nickname).isPresent();
    }

    // 查询所有用户
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

    // 根据用户 ID 查找用户
    public Optional<User> findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    // 更新用户信息
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    // 删除用户
    public void deleteUserById(Integer id) {
        userMapper.deleteUserById(id);
    }
    // 验证用户是否为管理员
    public boolean isAdmin(User user) {
        return user != null && UserRole.ADMIN.name().equals(user.getRole());
    }

    public User getUserByUsername(String username){
        return userMapper.getUserByUsername(username );
    }
    public User updateUserByUsername(String username, String password, String nickname) {
        // 直接更新密码（不加密）
        userMapper.updateUserByUsername(username, password, nickname);
        return userMapper.getUserByUsername(username);
    }
    public User changeUserAvatar(String username,String avatar) {
        // 执行更新操作
        userMapper.changeUserAvatar(username,avatar);
        return userMapper.getUserByUsername(username);
    }
    public User changeUserPassword(String username,String password) {
        // 执行更新操作
        userMapper.changeUserPassword(username,password);
        return userMapper.getUserByUsername(username);
    }
    public boolean recharge(String username, double amount) {
        int rowsAffected = userMapper.updateMoney(username, amount);
        return rowsAffected > 0;
    }
    public User pay(String username, BigDecimal money) {
        User user = userMapper.getUserByUsername(username);
        if (user != null) {
            boolean payResult = user.pay(money);
            if (payResult) {
                userMapper.updateUserMoney(username, user.getMoney());
            }
        }
        return user;
    }

    // 新增的检测用户金钱的方法
    public BigDecimal checkMoney(String username) {
        return userMapper.checkMoney(username);
    }

}